package com.xianzhiOA.controller;

import com.xianzhiOA.bean.XzEmp;
import com.xianzhiOA.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 登录controller
 *
 * @author 郭云春晓
 * @create 2017-04-17 17:14
 **/
@Controller
@RequestMapping("/Login")
@SessionAttributes({"userForeEnd","userLogin"})//userForeEnd前台登录session、userLogin后台登录session
public class LoginController {
    @Autowired
    private LoginService loginService;

    //后台登录
    @RequestMapping("login.emp")
    public ModelAndView login(String username, String password){
        ModelAndView mv;
        //数据库中emp
        XzEmp xzEmp = loginService.login(username);

        //登录人
        XzEmp loginP = new XzEmp();
        loginP.setEmpName(username);
        loginP.setEmpPwd(password);
        String msg = "";
        if(xzEmp!=null){
            if (xzEmp.getEmpPwd().equals(password)){
                msg = "登录成功";
                mv = new ModelAndView("backEnd/mainBack");
                mv.addObject("userLogin",xzEmp);
                mv.addObject("msg",msg);
            }else {
                msg = "密码错误";
                mv = new ModelAndView("redirect:goLogin");
                mv.addObject("msg",msg);
            }
        }else{
            msg = "用户不存在";
            mv = new ModelAndView("redirect:goLogin");
            mv.addObject("msg",msg);
        }
        return mv;
    }

    //跳转后台登录页面
    @RequestMapping("goLogin")
    public ModelAndView goLogin(HttpServletRequest request, HttpServletResponse response, HttpSession httpSession){
        ModelAndView mv = new ModelAndView("backEnd/loginBack");
        return mv;
    }

    //后台用户注销
    @RequestMapping("logout.emp")
    public ModelAndView logoutBackEnd(SessionStatus sessionStatus){
        ModelAndView mv = new ModelAndView("redirect:goLogin");
        sessionStatus.setComplete();//清空session
        return mv;
    }


    //前台登录
    @RequestMapping("/login.do")
    public ModelAndView loginForeEnd(String username, String password, String rememberMe,HttpServletResponse response){
        ModelAndView mv = new ModelAndView("foreEnd/login");
        //数据库中emp
        XzEmp xzEmp = loginService.login(username);
        String msg = "";
        if(xzEmp!=null){
            if (xzEmp.getEmpPwd().equals(password)){
                mv.addObject("userForeEnd",xzEmp);
                //记住登录状态
                if(rememberMe!=null&&rememberMe.equals("on")){
                    Cookie cookieName = new Cookie("cookieName",username);
                    Cookie cookiePwd = new Cookie("cookiePwd",password);
                    cookieName.setPath(System.getProperty("xianzhi.path"));
                    cookiePwd.setPath(System.getProperty("xianzhi.path"));
                    cookieName.setMaxAge(60*10);
                    cookiePwd.setMaxAge(60*10);
                    response.addCookie(cookieName);
                    response.addCookie(cookiePwd);
                }
                mv.setViewName("foreEnd/index");
            }else {
                msg = "密码错误";
                mv.addObject("msg",msg);
            }
        }else{
            msg = "用户不存在";
            mv.addObject("msg",msg);
        }
        return  mv;
    }

    //前台用户注销
    @RequestMapping("logout.do")
    public ModelAndView logoutForeEnd(SessionStatus sessionStatus){
        ModelAndView mv = new ModelAndView("foreEnd/login");
        sessionStatus.setComplete();//清空session
        return mv;
    }


    //跳转到主页
    @RequestMapping("goHomePage")
    public ModelAndView goHomePage(HttpServletRequest request, HttpServletResponse response, HttpSession httpSession){
        ModelAndView mv = new ModelAndView("backEnd/mainBack");
        return mv;
    }


}
